﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.Common;
using WMSADBUtility;
using WMSAML.SY;

namespace WMSADL.SY
{
    public class TUserImag_DAL
    {
        /// <summary>
        /// 
        /// </summary>
        public bool Exists(int ID)
        {
            Database db = DatabaseFactory.CreateDatabase();
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) from TUserImg where ID=@ID ");
            DbCommand dbCommand = db.GetSqlStringCommand(strSql.ToString());
            db.AddInParameter(dbCommand, "ID", DbType.Int32, ID);
            int cmdresult;
            object obj = db.ExecuteScalar(dbCommand);
            if ((Object.Equals(obj, null)) || (Object.Equals(obj, System.DBNull.Value)))
            {
                cmdresult = 0;
            }
            else
            {
                cmdresult = int.Parse(obj.ToString());
            }
            if (cmdresult == 0)
            {
                return false;
            }
            else
            {
                return true;
            }
        }


        /// <summary>
        /// 
        /// </summary>
        public int Add(TUserImagEntity entity)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("insert into TUserImg(");
            strSql.Append("UserID,Img,AddTime,Field1,Field2,Field3,Field4,Field5)");

            strSql.Append(" values (");
            strSql.Append("@UserID,@Img,@AddTime,@Field1,@Field2,@Field3,@Field4,@Field5)");
            strSql.Append(";select @@IDENTITY");
            Database db = DatabaseFactory.CreateDatabase();
            DbCommand dbCommand = db.GetSqlStringCommand(strSql.ToString());
            db.AddInParameter(dbCommand, "UserID", DbType.Int32, entity.UserID);
            db.AddInParameter(dbCommand, "Img", DbType.Binary, entity.Img);
            db.AddInParameter(dbCommand, "AddTime", DbType.String, entity.AddTime);
            db.AddInParameter(dbCommand, "Field1", DbType.AnsiString, entity.Field1);
            db.AddInParameter(dbCommand, "Field2", DbType.AnsiString, entity.Field2);
            db.AddInParameter(dbCommand, "Field3", DbType.AnsiString, entity.Field3);
            db.AddInParameter(dbCommand, "Field4", DbType.AnsiString, entity.Field4);
            db.AddInParameter(dbCommand, "Field5", DbType.AnsiString, entity.Field5);
            int result;
            object obj = db.ExecuteScalar(dbCommand);
            if (!int.TryParse(obj.ToString(), out result))
            {
                return 0;
            }
            return result;
        }
        /// <summary>
        /// 更新一条数据
        /// </summary>
        public bool Update(TUserImagEntity entity)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update TUserImg set ");
            strSql.Append("UserID=@UserID,");
            strSql.Append("Img=@Img,");
            strSql.Append("AddTime=@AddTime,");
            strSql.Append("Field1=@Field1,");
            strSql.Append("Field2=@Field2,");
            strSql.Append("Field3=@Field3,");
            strSql.Append("Field4=@Field4,");
            strSql.Append("Field5=@Field5");
            strSql.Append(" where ID=@ID ");
            Database db = DatabaseFactory.CreateDatabase();
            DbCommand dbCommand = db.GetSqlStringCommand(strSql.ToString());
            db.AddInParameter(dbCommand, "ID", DbType.Int32, entity.ID);
            db.AddInParameter(dbCommand, "UserID", DbType.Int32, entity.UserID);
            db.AddInParameter(dbCommand, "Img", DbType.Binary, entity.Img);
            db.AddInParameter(dbCommand, "AddTime", DbType.String, entity.AddTime);
            db.AddInParameter(dbCommand, "Field1", DbType.String, entity.Field1);
            db.AddInParameter(dbCommand, "Field2", DbType.String, entity.Field2);
            db.AddInParameter(dbCommand, "Field3", DbType.String, entity.Field3);
            db.AddInParameter(dbCommand, "Field4", DbType.String, entity.Field4);
            db.AddInParameter(dbCommand, "Field5", DbType.String, entity.Field5);
            int rows = db.ExecuteNonQuery(dbCommand);

            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        /// <summary>
        /// 
        /// </summary>
        public bool Delete(int ID)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from TUserImg ");
            strSql.Append(" where ID=@ID ");
            Database db = DatabaseFactory.CreateDatabase();
            DbCommand dbCommand = db.GetSqlStringCommand(strSql.ToString());
            db.AddInParameter(dbCommand, "ID", DbType.Int32, ID);
            int rows = db.ExecuteNonQuery(dbCommand);

            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        /// <summary>
        /// 
        /// </summary>
        public TUserImagEntity Getentity(int ID)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("select ID,UserID,Img,AddTime,Field1,Field2,Field3,Field4,Field5 from TUserImg ");
            strSql.Append(" where ID=@ID ");
            Database db = DatabaseFactory.CreateDatabase();
            DbCommand dbCommand = db.GetSqlStringCommand(strSql.ToString());
            db.AddInParameter(dbCommand, "ID", DbType.Int32, ID);
            TUserImagEntity entity = null;
            using (IDataReader dataReader = db.ExecuteReader(dbCommand))
            {
                if (dataReader.Read())
                {
                    entity = ReaderBind(dataReader);
                }
            }
            return entity;
        }


        /// <summary>
        /// 
        /// </summary>
        public TUserImagEntity GetEntityByUID(int UID)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select ID,UserID,Img,AddTime,Field1,Field2,Field3,Field4,Field5 from TUserImg ");
            strSql.Append(" where UserID=@UserID ");
            Database db = DatabaseFactory.CreateDatabase();
            DbCommand dbCommand = db.GetSqlStringCommand(strSql.ToString());
            db.AddInParameter(dbCommand, "UserID", DbType.Int32, UID);
            TUserImagEntity entity = null;
            using (IDataReader dataReader = db.ExecuteReader(dbCommand))
            {
                if (dataReader.Read())
                {
                    entity = ReaderBind(dataReader);
                }
            }
            return entity;
        }
        /// <summary>
        /// 对象实体绑定数据
        /// </summary>
        public TUserImagEntity ReaderBind(IDataReader dataReader)
        {
            TUserImagEntity entity = new TUserImagEntity();
            object ojb;
            ojb = dataReader["ID"];
            if (ojb != null && ojb != DBNull.Value)
            {
                entity.ID = (int)ojb;
            }
            ojb = dataReader["UserID"];
            if (ojb != null && ojb != DBNull.Value)
            {
                entity.UserID = (int)ojb;
            }
            ojb = dataReader["Img"];
            if (ojb != null && ojb != DBNull.Value)
            {
                entity.Img = (byte[])ojb;
            }
            ojb = dataReader["AddTime"];
            if (ojb != null && ojb != DBNull.Value)
            {
                entity.AddTime = (DateTime)ojb;
            }
            entity.Field1 = dataReader["Field1"].ToString();
            entity.Field2 = dataReader["Field2"].ToString();
            entity.Field3 = dataReader["Field3"].ToString();
            entity.Field4 = dataReader["Field4"].ToString();
            entity.Field5 = dataReader["Field5"].ToString();
            return entity;
        }

        /// <summary>
        /// 
        /// </summary>
        public TUserImagEntity DataRowToentity(DataRow row)
        {
            TUserImagEntity entity = new TUserImagEntity();
            if (row != null)
            {
                if (row["ID"] != null && row["ID"].ToString() != "")
                {
                    entity.ID = int.Parse(row["ID"].ToString());
                }
                if (row["UserID"] != null && row["UserID"].ToString() != "")
                {
                    entity.UserID = int.Parse(row["UserID"].ToString());
                }
                if (row["Img"] != null && row["Img"].ToString() != "")
                {
                    entity.Img = (byte[])row["Img"];
                }
                if (row["AddTime"] != null && row["AddTime"].ToString() != "")
                {
                    entity.AddTime = DateTime.Parse(row["AddTime"].ToString());
                }
                if (row["Field1"] != null)
                {
                    entity.Field1 = row["Field1"].ToString();
                }
                if (row["Field2"] != null)
                {
                    entity.Field2 = row["Field2"].ToString();
                }
                if (row["Field3"] != null)
                {
                    entity.Field3 = row["Field3"].ToString();
                }
                if (row["Field4"] != null)
                {
                    entity.Field4 = row["Field4"].ToString();
                }
                if (row["Field5"] != null)
                {
                    entity.Field5 = row["Field5"].ToString();
                }
            }
            return entity;
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataSet GetList(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select ID,UserID,Img,AddTime,Field1,Field2,Field3,Field4,Field5 ");
            strSql.Append(" FROM TUserImg ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            Database db = DatabaseFactory.CreateDatabase();
            return db.ExecuteDataSet(CommandType.Text, strSql.ToString());
        }

    }
}
